package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.UserDao;
import ysq.xyg.model.User;
import ysq.xyg.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Resource
    private UserDao userDao;
    @Autowired
    public void setUserDao(UserDao userDao) {
        this.baseDao = userDao;
    }

    //分页查询
    public PageInfo<User> pageInfo(User user, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(user!=null){
            if(StringUtil.isNotEmpty(user.getUserName())){
                criteria.andLike("userName","%"+user.getUserName()+"%");
            }

        }
        List<User> list=userDao.selectByExample(example);
        return new PageInfo<User>(list);
    }

    public PageInfo<User> pageInfoLeft(User user, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list=userDao.selectListLeft(user);
        return new PageInfo<User>(list);
    }
    public List<User> listLeft(User user) {
        List<User> list=userDao.selectListLeft(user);
        return list;
    }
    public User selectById(String id) {
        User user=new User();
        user.setOpenid(id);
        List<User> list=userDao.selectListLeft(user);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
    public User selectByName(String name) {
        User user=new User();
        user.setUserName(name);
        //通用Example查询
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(user!=null){
            if(StringUtil.isNotEmpty(user.getUserName())){
                criteria.andEqualTo("userName",name);
            }

        }
        List<User> list=userDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
    public int selectUserCount(User user){
        return userDao.selectUserCount(user);
    }
}
